package com.wz.redis;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisClient {
		
	private Jedis jedis; //非切片额客服端连接
	private JedisPool jedisPool;//非切片连接池
	private ShardedJedis shardedJedis;//切片额客服端连接
	private ShardedJedisPool shardedJedisPool;//切片额客户端连接池
	
	public void show(){
		System.out.println(jedis.set("10", "www"));
		System.out.println(jedis.set("101", "values2"));
		System.out.println(jedis.set("102", "values2"));
		System.out.println(jedis.set("103", "values2"));
		System.out.println(jedis.set("104", "values2"));
		Set<String> keys =jedis.keys("*"); 
		
		Iterator<String> iterator = keys.iterator();
		while(iterator.hasNext()){
			String key = iterator.next();
			System.out.println(key);
		}
		System.out.println(jedis.get("sectionlist"));
		
	}
	
	public RedisClient(){
		initPool();
		initShardePool();
		jedis =jedisPool.getResource();
		shardedJedis = shardedJedisPool.getResource();
	}
	
	
	/*
	 * 初始化非切片池
	 */
	private void initPool(){
		//池基本配置
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxIdle(300);
		config.setMaxIdle(100);
		config.setMaxWaitMillis(10001);
		config.setTestOnBorrow(true);
		config.setTestOnReturn(true);
		jedisPool = new JedisPool(config,"127.0.0.1",6379);
	}
	
	/*
	 * 初始化切片池
	 */
	
	private void initShardePool(){
		//池基本配置
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxIdle(20);
		config.setMaxIdle(5);
		config.setMaxWaitMillis(10001);
		config.setTestOnBorrow(false);
		
		//slave链接
		List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
		shards.add(new JedisShardInfo("127.0.0.1",6379,"master"));
		
		//构造池
		
		shardedJedisPool = new ShardedJedisPool(config,shards);
		
	}
	
	
	
	
	
	
	
	
	
	
	
}
